package com.csyaonie.android.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.csyaonie.android.R;

public class CustomDialog extends Dialog implements View.OnClickListener{

    private TextView d_title;
    private TextView d_msg;
    private TextView d_bt_cancel;
    private TextView d_bt_confirm;
    private String title,msg,cancel,confirm;
    private IConfirmClickListener confirmClickListener;
    private ICancelClickListener cancelClickListener;


    public CustomDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_dialog_layout);
        //设置宽度
        WindowManager m=getWindow().getWindowManager();
        Display d=m.getDefaultDisplay();
        WindowManager.LayoutParams p=getWindow().getAttributes();
        Point size=new Point();
        d.getSize(size);
        p.width= (int) (size.x*0.8);
        getWindow().setAttributes(p);

        d_title=findViewById(R.id.d_title);
        d_msg=findViewById(R.id.d_msg);
        d_bt_cancel=findViewById(R.id.d_bt_cancel);
        d_bt_confirm=findViewById(R.id.d_bt_confirm);
        if(!TextUtils.isEmpty(title)){
            d_title.setText(title);
        }
        if(!TextUtils.isEmpty(msg)){
            d_msg.setText(msg);
        }
        if(!TextUtils.isEmpty(cancel)){
            d_bt_cancel.setText(cancel);
        }
        if(!TextUtils.isEmpty(confirm)){
            d_bt_confirm.setText(confirm);
        }
        d_bt_cancel.setOnClickListener(this);
        d_bt_confirm.setOnClickListener(this);
    }

    public CustomDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public CustomDialog setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public CustomDialog setCancel(String cancel,ICancelClickListener cancelClickListener) {
        this.cancel = cancel;
        this.cancelClickListener=cancelClickListener;
        return this;
    }

    public CustomDialog setConfirm(String confirm,IConfirmClickListener confirmClickListener) {
        this.confirm = confirm;
        this.confirmClickListener=confirmClickListener;
        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.d_bt_cancel:
                if(cancelClickListener!=null){
                    this.cancelClickListener.onClick();
                }
                dismiss();
                break;
            case R.id.d_bt_confirm:
                if(confirmClickListener!=null){
                    this.confirmClickListener.onClick();
                }
                dismiss();
                break;
        }
    }

    public interface IConfirmClickListener{
        void onClick();
    }

    public interface ICancelClickListener{
        void onClick();
    }
}
